package com.htj.interceptor;

import com.htj.common.ResultCodeEnum;
import com.htj.exception.TokenException;
import com.htj.util.JWTUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("我是前置拦截器");
        try {
            JWTUtil.verify(request.getHeader("token"));
            return true;
        }catch (Exception e){
            throw new TokenException(null,ResultCodeEnum.TOKEN_ERROR);
        }
    }
}
